﻿
///////////////////////////////////////////////////////////
//This code is taken from the Channel9 video tutorials.
//They are here for convenience - check the web link in the
//folder for each of these to see the original postings.
//
//These challenges are completed - the uncompleted ones are in
//a subfolder below this one, named OriginalCode
//////////////////////////////////////////////////////////////

using System;
using System.Reactive.Subjects;
using System.Reactive.Linq;

namespace IntroductionToRx
{
    class MyObservables
    {
        ISubject<string> textChanged = new Subject<string>();
        int lastLen;

        public virtual void OnTextChanged(string text)
        {
            textChanged.OnNext(text);
        }

        public IObservable<string> TextChanged
        {
            get
            {
                return textChanged;
            }
        }

        public IObservable<int> LengthChanged
        {
            get
            { // Compose new "event" by filtering existing event.
                return textChanged
                    .Where(s => s.Length != lastLen)
                    .Do(s => lastLen = s.Length)
                    .Select(s => s.Length);
            }
        }
    }
}
